using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DataWasher.WebApi.Repositories;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Report.WebApi.Entities;
using Report.WebApi.Interfaces;
using Report.WebApi.Repositories.Base;
using Report.WebApi.Services;
using Report.WebApi.Services.Common;
using ToolManager.Common;
using ToolManager.IService;
using ToolManager.Service;

namespace Report.WebApi
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }
        public readonly string ganweiCosr = "AllowSpecificOrigins";
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            var sqlserverConnStr = Configuration.GetConnectionString("sqlserver");
            services.AddDbContext<DBContext>(options => options.UseSqlServer(sqlserverConnStr));


            #region register services
            services.AddScoped<IQuerySQXService, QuerySQXService>();
            services.AddScoped<IStorageService, StorageService>();
            services.AddSingleton<ConnectionStrings>();
            services.AddScoped<IQueryWCLService, QueryWCLService>();
            services.AddScoped<IQueryCQLService, QueryCQLService>();
            services.AddScoped<IQueryWXSQSService, QueryWXSQSService>();
            services.AddScoped<IQuerySPSCService, QuerySPSCService>();
            services.AddScoped<IQueryDPSQService, QueryDPSQService>();
            services.AddScoped<IQueryWXLBService, QueryWXLBService>();
            services.AddScoped<IQueryWXZLService, QueryWXZLService>();
            services.AddScoped<IQueryXYDService, QueryXYDService>();
            services.AddScoped<IQueryWXFYService, QueryWXFYService>();
            services.AddScoped<IQueryGYSWXFYService, QueryGYSWXFYService>();
            services.AddScoped<IQueryWXLBFYService, QueryWXLBFYService>();
            services.AddScoped<IQueryWXLXService, QueryWXLXService>();
            services.AddScoped<IQueryDPLXService, QueryDPLXService>();
            services.AddScoped<IQueryDPBQService, QueryDPBQService>();
            services.AddScoped<IQueryBrandService, QueryBrandService>();
            services.AddScoped<IQueryCBLXService, QueryCBLXService>();
            services.AddScoped<IQueryZZService, QueryZZService>();
            services.AddScoped<ICommonService, CommonService>();
            services.AddScoped<IQueryWXWTYWFLService, QueryWXWTYWFLService>();
            #endregion

            #region register repository
            services.AddScoped(typeof(IRepositoryBase<>), typeof(RepositoryBase<>));
            services.AddScoped<ISysDomainRepository, SysDomainRepository>();
            #endregion

            services.AddCors(options =>
            {
                options.AddPolicy(ganweiCosr,
                builder => builder.AllowAnyHeader()
                .AllowAnyMethod()
                .AllowAnyOrigin());
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            ServiceLocator.Instance = app.ApplicationServices;
            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseCors(ganweiCosr);
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}
